package com.ruoyi.btlfbreed.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.btlfbreed.domain.TbStockLog;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.btlfbreed.domain.TbMedicineInfo;
import com.ruoyi.btlfbreed.service.ITbMedicineInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 药品信息Controller
 * 
 * @author codeman
 * @date 2024-05-04
 */
@RestController
@RequestMapping("/btlfbreed/medicineInfo")
public class TbMedicineInfoController extends BaseController
{
    @Autowired
    private ITbMedicineInfoService tbMedicineInfoService;

    /**
     * 查询药品信息列表
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:medicineInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbMedicineInfo tbMedicineInfo)
    {
        startPage();
        List<TbMedicineInfo> list = tbMedicineInfoService.selectTbMedicineInfoList(tbMedicineInfo);
        return getDataTable(list);
    }

    /**
     * 导出药品信息列表
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:medicineInfo:export')")
    @Log(title = "药品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbMedicineInfo tbMedicineInfo)
    {
        List<TbMedicineInfo> list = tbMedicineInfoService.selectTbMedicineInfoList(tbMedicineInfo);
        ExcelUtil<TbMedicineInfo> util = new ExcelUtil<TbMedicineInfo>(TbMedicineInfo.class);
        util.exportExcel(response, list, "药品信息数据");
    }

    /**
     * 获取药品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:medicineInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbMedicineInfoService.selectTbMedicineInfoById(id));
    }

    /**
     * 新增药品信息
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:medicineInfo:add')")
    @Log(title = "药品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbMedicineInfo tbMedicineInfo)
    {
        BigDecimal stockQuantity = tbMedicineInfo.getStockQuantity(); //库存

        BigDecimal reduce = tbMedicineInfo.getTbStockLogList().stream().filter(p->p.getOperationType().equals("out")).map(TbStockLog::getQuantity)
            .reduce(BigDecimal.ZERO, BigDecimal::add);
        int comparisonResult = stockQuantity.compareTo(reduce);

        if (comparisonResult<0){
            return AjaxResult.error("超出库存数量！");
        }
//        else
//        {
//            tbMedicineInfo.setStockQuantity(stockQuantity.subtract(reduce));
//        }
        return toAjax(tbMedicineInfoService.insertTbMedicineInfo(tbMedicineInfo));
    }

    /**
     * 修改药品信息
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:medicineInfo:edit')")
    @Log(title = "药品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbMedicineInfo tbMedicineInfo)
    {
        BigDecimal stockQuantity = tbMedicineInfo.getStockQuantity(); //库存

        BigDecimal reduce = tbMedicineInfo.getTbStockLogList().stream().filter(p->p.getOperationType().equals("out")).map(TbStockLog::getQuantity)
            .reduce(BigDecimal.ZERO, BigDecimal::add);
        int comparisonResult = stockQuantity.compareTo(reduce);

        if (comparisonResult<0){
            return AjaxResult.error("超出库存数量！");
        }
//        else
//        {
//            tbMedicineInfo.setStockQuantity(stockQuantity.subtract(reduce));
//        }
        return toAjax(tbMedicineInfoService.updateTbMedicineInfo(tbMedicineInfo));
    }

    /**
     * 删除药品信息
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:medicineInfo:remove')")
    @Log(title = "药品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbMedicineInfoService.deleteTbMedicineInfoByIds(ids));
    }
}
